﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace System.Qizx.Extensions {
    public static class IOExtensions {

        public static System.IO.FileSystemInfo[] GetFiles(this System.IO.FileSystemInfo path) {
            if ((path.Attributes & System.IO.FileAttributes.Directory) > 0) {
                String[] fullpathnames = System.IO.Directory.GetFileSystemEntries(path.FullName);
                System.IO.FileSystemInfo[] result = new System.IO.FileSystemInfo[fullpathnames.Length];
                for (int i = 0; i < result.Length; i++) {
                    if (File.Exists(fullpathnames[i]))
                        result[i] = new System.IO.FileInfo(fullpathnames[i]);
                    else
                        result[i] = new System.IO.DirectoryInfo(fullpathnames[i]);
                }
                return result;
            } else {
                return null;
            }
        }

        /// <summary>
        /// Creates a new empty file with the specified pathname.
        /// </summary>
        /// <param name="path">The abstract pathname of the file</param>
        /// <returns>True if the file does not exist and was succesfully created</returns>
        public static bool CreateNewFile(this System.IO.FileInfo path) {
            if (path.Exists) {
                return false;
            } else {
                using (System.IO.FileStream createdFile = path.Create()) {
                    return true;
                }
            }
        }

        /// <summary>
        /// Compares the specified object with the specified path
        /// </summary>
        /// <param name="path">An abstract pathname to compare with</param>
        /// <param name="file">An object to compare with the given pathname</param>
        /// <returns>A value indicating a lexicographically comparison of the parameters</returns>
        public static int CompareTo(this System.IO.FileInfo path, System.Object file) {
            if (file is System.IO.FileInfo) {
                System.IO.FileInfo fileInfo = (System.IO.FileInfo)file;
                return path.FullName.CompareTo(fileInfo.FullName);
            } else {
                throw new System.InvalidCastException();
            }
        }

        /// <summary>
        /// Sets the last modified time of the specified file with the specified value.
        /// </summary>
        /// <param name="file">The file to change it last-modified time</param>
        /// <param name="date">Total number of miliseconds since January 1, 1970 (new last-modified time)</param>
        /// <returns>True if the operation succeeded, false otherwise</returns>
        public static bool SetLastModified(this System.IO.FileInfo file, long date) {
            try {
                long valueConstant = (new System.DateTime(1969, 12, 31, 18, 0, 0)).Ticks;
                file.LastWriteTime = new System.DateTime((date * 10000L) + valueConstant);
                return true;
            } catch (System.Exception exception) {
                String exceptionMessage = exception.Message;
                return false;
            }
        }

        /// <summary>
        /// Returns true if the file specified by the pathname is a hidden file.
        /// </summary>
        /// <param name="file">The abstract pathname of the file to test</param>
        /// <returns>True if the file is hidden, false otherwise</returns>
        public static bool IsHidden(this System.IO.FileInfo file) {
            return ((file.Attributes & System.IO.FileAttributes.Hidden) > 0);
        }

        /// <summary>
        /// Creates an instance of System.Uri class with the pech specified
        /// </summary>
        /// <param name="path">The abstract path name to create the Uri</param>
        /// <returns>A System.Uri instance constructed with the specified path</returns>
        public static System.Uri ToUri(this System.IO.FileSystemInfo path) {
            System.UriBuilder uri = new System.UriBuilder();
            uri.Path = path.FullName;
            uri.Host = String.Empty;
            uri.Scheme = System.Uri.UriSchemeFile;
            return uri.Uri;
        }

        /// <summary>
        /// Gets the length of file, if file not exists return 0.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <returns></returns>
        public static long GetLength(this FileSystemInfo file) {
            FileInfo fileInfo = file as FileInfo;
            if (fileInfo != null && file.Exists)
                return fileInfo.Length;
            else
                return 0;
        }
    }
}
